//
//  SUNTabBarController.swift
//  SUNweibo
//
//  Created by sighting on 16/8/20.
//  Copyright © 2016年 sighting. All rights reserved.
//

import UIKit

class SUNTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // setChildViewCtroller
        setChildViewCtroller()
        // 添加按钮
        addComposeBnt()
    }
   
    // 懒加载composeBnt
    lazy var composeBnt = UIButton().setImageWithBackgroundImage(imageName: "compose_icon_add", backgroundImageName: "compose_button")
    
    // 撰写按钮事件
    @objc private func composeSelector(){
        
    print("撰写内容")
    }
    
    // 支持横屏
    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return .portrait
    }
}

// MARK:ChildViewCtrollers
extension SUNTabBarController{
    
    // addComposeBnt
    private func addComposeBnt()->(){
    
        guard let count = viewControllers?.count
            else { return  }
   
        // compose的frame
        let wid = tabBar.bounds.width/CGFloat(count) - 1
        self.composeBnt.frame = tabBar.bounds.insetBy(dx: 2*wid, dy: 0)
        
        // 按钮添加方法
        self.composeBnt.addTarget(self, action: #selector(composeSelector), for: .touchUpInside)
        self.tabBar.addSubview(self.composeBnt)
    }
    
    private func setChildViewCtroller(){
        
        /// 添加自控制器
        let arr = [
                   ["title":"首页","clsName":"SUNHomeController","imageName":"home"],
                   ["title":"消息","clsName":"SUNMessageController","imageName":"message_center"],
                   ["clsName":"SUNBaseViewController"],
                   ["title":"发现","clsName":"SUNDiscovery","imageName":"discover"],
                   ["title":"我的","clsName":"SUNProfile","imageName":"profile"],
                  ]
        
        /// Description
        var arrM = [UIViewController]()
        for dict in arr {
            
           arrM.append(ChildViewCtrollers(dict: dict))
        }
        viewControllers = arrM
    }
    
    /// addChildViewCtrollers
    /** - parameter  title标题,
                     clsName类名,
                     imageName图片名称
     */
    private func ChildViewCtrollers(dict:[String:String])->UIViewController{
        
        guard let title = dict["title"],
              let clsName = dict["clsName"],
              let imageName = dict["imageName"],
              let cls = NSClassFromString(Bundle.main.nameSpace()+"."+clsName) as?UIViewController.Type
        else { return UIViewController() }
        
        // 创建子控制器
        let vc = cls.init()
        vc.title = title
        // 添加image
        vc.tabBarItem.image = UIImage(named: "tabbar_"+imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_"+imageName+"_selected")?.withRenderingMode(.alwaysOriginal)
        
       // vc.view.backgroundColor = UIColor.colorWithRandom()
        
        //vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 24)], for: .normal)
        // 设置tabBarItem文字颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange()], for: .selected)
        
        // 添加NavigtionController
        let nav = SUNNavigtionController(rootViewController: vc)
        return nav
    }
}
